package com.lyfei.yd.admin.web;

import com.lyfei.yd.common.core.domain.AjaxResult;
import com.lyfei.yd.domain.dto.SpuDTO;
import com.lyfei.yd.domain.dto.SpuPageQueryDto;
import com.lyfei.yd.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("/spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    @PostMapping("/add")
    public AjaxResult addSpu(@RequestBody SpuDTO dto) {
        spuService.create(dto);
        return AjaxResult.success();
    }

    @PostMapping("/update/{id}")
    public AjaxResult updateSpu(@RequestBody SpuDTO dto, @PathVariable Long id) {
        spuService.update(dto, id);
        return AjaxResult.success();
    }

    @GetMapping("/page")
    public AjaxResult page(SpuPageQueryDto dto) {
        return AjaxResult.success(spuService.page(dto));
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        spuService.delete(id);
        return AjaxResult.success();
    }

    @GetMapping("/all")
    public AjaxResult getAllSpu() {
        return AjaxResult.success(spuService.list());
    }

    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return AjaxResult.success(spuService.detail(id));
    }

    @GetMapping("/specKeys/{spuId}")
    public AjaxResult getSpecKeys(@PathVariable Long spuId) {
        return AjaxResult.success(spuService.getSpecKeys(spuId));
    }

}
